package com.intranet.fund.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.intranet.fund.domain.Blacklist;
import com.intranet.fund.service.BlacklistService;
import com.water.dao.Page;
import com.water.domain.DataTableParameter;
import com.water.tool.DateUtil;
import com.water.web.Tool;

@Controller
public class BlacklistController {

	@Autowired
	private BlacklistService blacklistService;

	@RequestMapping("/getAllBlacklist")
	public void getAllBlacklist(HttpServletResponse response,
			DataTableParameter param) {

		// 搜索条件
		String search = param.getSSearch();
		// 查询结果集
		Page page = blacklistService.getAllBlacklist(param.getPageNo(), param
				.getIDisplayLength(), search);
		List<Blacklist> blacklists = page.getData();
		List<Object> list = new ArrayList<Object>();
		for (Blacklist b : blacklists) {
			list.add(new String[] {
					"<input name=\"checkbox\" type=\"checkbox\" class=\"checkboxes\" value=\""
							+ b.getId().toString() + "\" />", b.getIp(),
					b.getStartTime()});
		}
		// 封装页面参数
		String aaData = JSONArray.fromObject(list).toString();
		JSONObject json = new JSONObject();
		json.put(DataTableParameter.SECHO, param.getSEcho());
		json.put(DataTableParameter.ITOTALRECORDS, page.getTotalCount());// 过滤前总记录数
		json.put(DataTableParameter.ITOTALDISPALYRECORDS, page.getTotalCount());// 过滤后总记录数
		json.put(DataTableParameter.AADATA, aaData);
		Tool.writeByAction(json.toString(), response);
	}

	@RequestMapping("/loadBlacklist")
	public ModelAndView loadBlacklist(HttpServletRequest request,
			@RequestParam(value = "id", required = true)
			int id, @RequestParam(value = "type", required = true)
			String type) {
		ModelAndView mav = new ModelAndView();
		Blacklist blacklist = blacklistService.getBlacklistById(id);
		mav.addObject("blacklist", blacklist);
		if (type.equals("edit")) {
			mav.setViewName("/fund/blacklist/editBlacklist");
		} else {
			mav.setViewName("/fund/blacklist/showBlacklist");
		}
		return mav;
	}

	@RequestMapping("/addBlacklist")
	public ModelAndView addBlacklist(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/fund/blacklist/addBlacklist");
		return mav;
	}

	@RequestMapping("/saveBlacklist")
	public void saveBlacklist(HttpServletResponse response, Blacklist blacklist) {
		String msg = "N";
		if (blacklist.getId() == null) {
			Blacklist b = new Blacklist();
			b.setIp(blacklist.getIp());
			b.setStartTime(DateUtil.parseToString(new Date(),DateUtil.yyMMddHHmmss));
			blacklistService.saveBlacklist(b);
			msg = "Y";
		} else {
			Blacklist b = blacklistService.getBlacklistById(blacklist.getId());
			b.setIp(blacklist.getIp());
			blacklistService.updateBlacklist(b);
			msg = "Y";
		}
		JSONObject o = new JSONObject();
		o.put("Message", msg);
		o.put("ReturnUrl", msg);
		Tool.writeByAction(o.toString(), response);
	}

	@RequestMapping("/deleteBlacklist")
	public void deleteBlacklist(HttpServletResponse response,
			@RequestParam(value = "ids", required = false)
			String ids) {
		String msg = "";
		String[] id = ids.split(",");
		for (int i = 0; i < id.length; i++) {
			Blacklist b = blacklistService.getBlacklistById(Integer.valueOf(id[i]));
			msg = blacklistService.deleteBlacklist(b);
		}
		Tool.writeByAction(msg, response);
	}
}
